package com.example.tellwin.view;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.cncoderx.wheelview.Wheel3DView;
import com.example.tellwin.R;
import com.example.tellwin.base.BaseDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : LinZePeng
 * @date : 2020/11/13
 * @Describe ：
 **/
public class SelectWheelDialog extends BaseDialog {

    private TextView cancelTv;
    private TextView titleTv;
    private TextView confrimBtn;
    private Wheel3DView middleWheelView;
    private String title;

    List<CharSequence>middleList = new ArrayList<>();

    private OnSelectListener onSelectListener;

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }


    public SelectWheelDialog(@NonNull Context context, List<CharSequence> middleList) {
        super(context , R.style.ActionSheetDialogStyle);
        this.middleList = middleList;
    }

    public void setMiddleList(List<CharSequence> middleList) {
        this.middleList = middleList;
        if(middleWheelView!=null){
            middleWheelView.setEntries(middleList);
        }
    }

    public void setTitle(String title){
        this.title = title;
        if(titleTv!=null){
            titleTv.setText(title);
        }
    }

    public SelectWheelDialog(@NonNull Context context) {
        super(context , R.style.ActionSheetDialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select_wheel);
        Window window = this.getWindow();
        //设置位置在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置弹入弹出动画
        window.setWindowAnimations(R.style.BottomDialog_Animation);
        //设置为全屏dialog
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;

        window.setAttributes(params);
        initView();
        initEvent();

        titleTv.setText(title);
    }

    private void initView() {
        cancelTv = (TextView) findViewById(R.id.cancel_tv);
        titleTv = (TextView) findViewById(R.id.title_tv);
        confrimBtn = (TextView) findViewById(R.id.confrim_btn);
        middleWheelView =  findViewById(R.id.middle_wheel_view);
        middleWheelView.setEntries(middleList);

    }

    private void initEvent(){
        cancelTv.setOnClickListener(v -> {
            dismiss();
        });
        confrimBtn.setOnClickListener(v -> {
            if(onSelectListener!=null){
                onSelectListener.onSelect(middleWheelView.getCurrentIndex() , middleWheelView.getCurrentItem().toString());
            }
            dismiss();
        });
    }

    public interface OnSelectListener{
        void onSelect(int position ,String str);
    }

    public interface OnAddressSelectListener{
        void onAddressSelect(String province , String city , String area);
    }

}
